<?php

require_once "Basic.php";

class ClientByEmail extends BasicEntity {

  public function __construct() {
    $this->fields = array (
      new FormField(true, "email", "Email", new FormFieldEmail())
    );
    $this->processFields();
  }
  
  public function actionDescription() {
    return "Search";
  }
  
  public function actionSearch() {
    $email = $this->fields['email']->value();
    
    $db = new DBUtils();
    $idCliente = null;
    try {
      $db->connect();
      
      $db->runQuery("select idCliente from DadosCliente where email = ?", array($email));
      $item = $db->getItem();
      if($item === null) {
        $db->close();
        return htmlentities("Não há cliente com esse e-mail.");
      }
      $idCliente = $item->idCliente;
      $db->close();
    }
    catch(DBError $e) {
      $db->close();
      logMessage($e->errors, "ClientByEmail");
      return "Falha interna do banco de dados. Erro foi reportado.";
    }
    header("Location: ClientInfo.php?idCliente=" . $item->idCliente);
    return true;
  }
  
}

mustBeEmployee();
correctStdFormGET();

$page = new Page("Buscar usu&aacute;rio");
$busca = new ClientByEmail();
$form = new StdForm($busca, $_GET, $_POST, "", "", "ClientByEmail.php");
$form->processForm();
$page->addContent($form->getHtml());

echo $page->getHtml();
